local skel = fk.CreateSkill {
  name = "rmt__xiaozhan",
}

local spec = {
  on_cost = function(self, event, target, player, data)
    local discard_data = {
      num = 2,
      min_num = 0,
      include_equip = true,
      skillName = skel.name,
      pattern = ".",
    }
    local success, dat = player.room:askToUseActiveSkill(player, {
      skill_name = "discard_skill",
      prompt = "#rmt__xiaozhan-invoke:::"..data.card:toLogString(),
      extra_data = discard_data,
      skip = true,
    })
    if success and dat then
      event:setCostData(self, {cards = dat.cards or {}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.extra_data = data.extra_data or {}
    if #event:getCostData(self).cards > 0 then
      data.extra_data.rmt__xiaozhan = {player.id, 1}
      room:throwCard(event:getCostData(self).cards, skel.name, player, player)
    else
      data.extra_data.rmt__xiaozhan = {player.id, 2}
      player:drawCards(2, skel.name)
    end
    local use = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if use ~= nil and Fk.skills["unexpectation_skill"] then --加一下判断，别哪天应变没了又要报错
      local new_card = Fk:cloneCard("unexpectation", data.card.suit, data.card.number)
      local c = table.simpleClone(data.card)
      for k, v in pairs(c) do
        if new_card[k] == nil then
          new_card[k] = v
        end
      end
      if data.card:isVirtual() then
        new_card.subcards = data.card.subcards
      else
        new_card.id = data.card.id
      end
      new_card.skillNames = data.card.skillNames
      new_card.skill = Fk.skills["unexpectation_skill"]
      data.card = new_card
      use.data.card = new_card
    end
  end,
}

skel:addEffect(fk.TargetSpecified, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data.card.trueName == "slash" and data.firstTarget
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

skel:addEffect(fk.TargetConfirmed, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data.card.trueName == "slash"
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

skel:addEffect(fk.CardUseFinished, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.rmt__xiaozhan and data.extra_data.rmt__xiaozhan[1] == player.id and
    not player.dead and not data.damageDealt
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("rmt__xiaozhan")
    if data.extra_data.rmt__xiaozhan[2] == 1 then
      room:notifySkillInvoked(player, "rmt__xiaozhan", "drawcard")
      player:drawCards(2, "rmt__xiaozhan")
    else
      room:notifySkillInvoked(player, "rmt__xiaozhan", "negative")
      room:askToDiscard(player, {
        min_num = 2,
        max_num = 2,
        skill_name = skel.name,
        include_equip = true,
        cancelable = false,
      })
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__xiaozhan"] = "骁战",
  [":rmt__xiaozhan"] = "当你指定或成为【杀】的目标后，你可以摸/弃置两张牌，令此牌按【出其不意】结算。然后若此牌没有造成伤害，"..
  "你弃置/摸两张牌。",
  ["#rmt__xiaozhan-invoke"] = "骁战：是否将此%arg改为【出其不意】结算？（弃两张牌，或直接点“确定”摸两张牌）",

  ["$rmt__xiaozhan1"] = "出城浪战，以卵击石，吾破之必矣。",
  ["$rmt__xiaozhan2"] = "闭城留死，待断其手足，旋踵可灭。",
}

return skel
